< previous page page_212 next page >

Page 212
'This code assumes the following:
        ' db is an open database with data
        ' rs is an open recordset with data
        ' lstdata is a listbox
        ' lblcount is a label

    Do While Not rs.EOF
        lstData.AddItem rs.Fields(0)
        lblCount = lstData.ListCount
        DoEvents
        rs.MoveNext
    Loop
By placing the While condition clause in the Do statement, you tell the program that you want to evaluate the condition before you run any statements inside the loop. If the condition is True, the repetitive statements between the Do statement and the Loop statement are run. Then the program returns to the Do statement to evaluate the condition again. As soon as the condition is False, the program moves to the statement following the Loop statement. Both the Do and the Loop statements must be present.
With this form of the loop, the statements inside the loop might never be run. If the condition is False before the loop is run the first time, the program just proceeds to the statements after the loop.
To run the Do While loop at least once, you must use the second form of the Do While loop. This form of the loop places the condition in the Loop statement. This placement tells the program that you want the loop to run at least once and then evaluate the condition to determine whether to repeat the loop:
Do 
        lstData.AddItem rs.Fields(0)
        lblCount = lstData.ListCount
        DoEvents
        rs.MoveNext
Loop While Not rs.EOF
Be careful when using the loop in such a manner because statements between the Do and Loop statements will be executed without first checking the loop condition. For example, if the EOF function already returns True before entering the loop, an error would occur when trying to access the current record.
CAUTION
Do not put the While condition clause in both the Do and the Loop statements because doing so causes an error when you try to run your program.

 
< previous page page_212 next page >